package com.kexun.servlet.teacher;

import com.kexun.pojo.Teacher;
import com.kexun.service.TeaService;
import com.kexun.service.impl.TeaServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/ServletEditTeacherInfo")
public class ServletEditTeacherInfo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        HttpSession session = request.getSession();

        Map<String, String[]> map = request.getParameterMap();
        Teacher teacher = new Teacher();
        try {
            BeanUtils.populate(teacher,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


        TeaService ts = new TeaServiceImpl();
        int i = ts.updateTea(teacher);
        System.out.println(i);
        if(i>0){
//            response.getWriter().print("修改成功");
//            response.setHeader("refresh","1.5;url=teacher/updStu.html");

//            response.setHeader("refresh","3;url=teacher/addScore.jsp");

            session.setAttribute("message","1");
            request.getRequestDispatcher("/ServletTeacherInfo").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
